package it.asvp.asvp_portal.data.dao.impl;


import it.asvp.asvp_portal.data.dao.AddressDao;
import it.asvp.asvp_portal.data.entity.domain.Address;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AddressDaoImpl implements AddressDao {

	private static final Log log = LogFactory.getLog(AddressDaoImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	public Long persist(Address transientInstance) {
		log.debug("persisting Address instance");
		Long id=null;
		try {
			id=(Long) sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}


	public void delete(Address persistentInstance) {
		log.debug("deleting Address instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Address merge(Address detachedInstance) {
		log.debug("merging Address instance");
		try {
			Address result = (Address) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Address findById(java.lang.Long id) {
		
		Criteria cr=sessionFactory.getCurrentSession().createCriteria(Address.class);
		cr.add(Restrictions.eq("id", id));
		Address a = (Address)cr.uniqueResult();
		return a;
	}
}
